import { createAction, createReducer, ActionType } from 'typesafe-actions';

export interface MainInfoState {
    word: string | null;
    wordSourceId: number | null;
}

export const setWord = createAction('MAIN_INFO/SET_WORD')<string | null>();
export const setWordSourceId = createAction('MAIN_INFO/SET_WORDSOURCEID')<number | null>();

export type Actions = ActionType<typeof setWord> | ActionType<typeof setWordSourceId>;

const initialState: MainInfoState = {
    word: null,
    wordSourceId: null
};

export const Reducer = createReducer<MainInfoState, Actions>(initialState)
    .handleAction(setWord, (state, action) => {
        const newState = { ...state }
        newState.word = action.payload
        return newState
    })
    .handleAction(setWordSourceId, (state, action) => {
        const newState = { ...state }
        newState.wordSourceId = action.payload
        return newState
    })